package com.sun.module_common.widget

import ToolbarHeight
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sun.module_base.ext.click
import com.sun.module_base.theme.color_666
import com.sun.module_base.theme.white

/**
 *@author:sunc
 *@time :2025/8/11 16:47
 *@desc: 描述
 */

@Composable
fun AppBar(
           title: String,
           modifier: Modifier = Modifier,
           rightText: String?= null,
           onBack: (() -> Unit)? = null,
           onRightClick: (() -> Unit)? = null,
           imageVector: ImageVector? = null,) {
    Box(
        modifier = modifier
            .fillMaxWidth().background(MaterialTheme.colorScheme.primary)
            .height(ToolbarHeight)
    ) {
        Row(modifier = Modifier.fillMaxSize()) {
            if (onBack != null) {
                Icon(
                    Icons.AutoMirrored.Default.ArrowBack,
                    null,
                    Modifier
                        .click(onClick = onBack)
                        .align(Alignment.CenterVertically)
                        .padding(12.dp),
                    tint = white
                )
            }
            Spacer(modifier = Modifier.weight(1f))
            if (!rightText.isNullOrEmpty() && imageVector == null) {
                Text(
                    text = rightText,
                    color = color_666,
                    modifier = Modifier
                        .align(Alignment.CenterVertically)
                        .padding(horizontal = 20.dp)
                        .click { onRightClick?.invoke() }
                )
            }

            if (imageVector != null) {
                Icon(
                    imageVector = imageVector,
                    contentDescription = null,
                    modifier = Modifier
                        .align(Alignment.CenterVertically)
                        .padding(end = 12.dp)
                        .click {
                            onRightClick?.invoke()
                        })
            }
        }
        Text(
            text = title,
            modifier = Modifier
                .align(Alignment.Center)
                .padding(horizontal = 40.dp),
            color = white,
            textAlign = TextAlign.Center,
            fontSize = if (title.length > 14) 12.sp else 16.sp,
            fontWeight = FontWeight.W500,
            maxLines = 1
        )

    }
}